package cn.iocoder.an.module.business.controller.admin.auditlevel;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.auditlevel.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.auditlevel.AuditLevelDO;
import cn.iocoder.an.module.business.service.auditlevel.AuditLevelService;

@Tag(name = "管理后台 - 审核等级")
@RestController
@RequestMapping("/business/audit-level")
@Validated
public class AuditLevelController {

    @Resource
    private AuditLevelService auditLevelService;

    @PostMapping("/create")
    @Operation(summary = "创建审核等级")
    @PreAuthorize("@ss.hasPermission('business:audit-level:create')")
    public CommonResult<Long> createAuditLevel(@Valid @RequestBody AuditLevelSaveReqVO createReqVO) {
        return success(auditLevelService.createAuditLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审核等级")
    @PreAuthorize("@ss.hasPermission('business:audit-level:update')")
    public CommonResult<Boolean> updateAuditLevel(@Valid @RequestBody AuditLevelSaveReqVO updateReqVO) {
        auditLevelService.updateAuditLevel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审核等级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:audit-level:delete')")
    public CommonResult<Boolean> deleteAuditLevel(@RequestParam("id") Long id) {
        auditLevelService.deleteAuditLevel(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除审核等级")
                @PreAuthorize("@ss.hasPermission('business:audit-level:delete')")
    public CommonResult<Boolean> deleteAuditLevelList(@RequestParam("ids") List<Long> ids) {
        auditLevelService.deleteAuditLevelListByIds(ids);
        return success(true);
    }


    @TransMethodResult
    @GetMapping("/get")
    @Operation(summary = "获得审核等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:audit-level:query')")
    public CommonResult<AuditLevelRespVO> getAuditLevel(@RequestParam("id") Long id) {
        AuditLevelDO auditLevel = auditLevelService.getAuditLevel(id);
        return success(BeanUtils.toBean(auditLevel, AuditLevelRespVO.class));
    }

    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得审核等级分页")
    @PreAuthorize("@ss.hasPermission('business:audit-level:query')")
    public CommonResult<PageResult<AuditLevelRespVO>> getAuditLevelPage(@Valid AuditLevelPageReqVO pageReqVO) {
        PageResult<AuditLevelDO> pageResult = auditLevelService.getAuditLevelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuditLevelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审核等级 Excel")
    @PreAuthorize("@ss.hasPermission('business:audit-level:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuditLevelExcel(@Valid AuditLevelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuditLevelDO> list = auditLevelService.getAuditLevelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "审核等级.xls", "数据", AuditLevelRespVO.class,
                        BeanUtils.toBean(list, AuditLevelRespVO.class));
    }

}